package com.ksframework.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

/**
 * 常用Bean工具集. <p>
 * 该类继承apache BeanUtils，可以自行扩展.<p>
 * @modify: 
 * @see org.apache.commons.beanutils.BeanUtils
 * @version 1.0
 */
public class BeanUtil extends BeanUtils{

	protected static Logger logger = LoggerFactory.getLogger(BeanUtils.class);
	
	/**
	 * 获取对象属性.
	 * 强制获取，无须通过get方法.
	 * @param object 对象
	 * @param fieldName 属性
	 * @return
	 * @throws NoSuchFieldException
	 */
	public static Object getFieldValue(Object object, String fieldName) throws NoSuchFieldException {
		Field field = getDeclaredField(object, fieldName);
		if (!field.isAccessible()) {
			field.setAccessible(true);
		}

		Object result = null;
		try {
			result = field.get(object);
		} catch (IllegalAccessException e) {
			logger.error("不可能抛出的异常{}", e.getMessage());
		}
		return result;
	}
	
	/**
	 * 设置对象属性值.
	 * 强制设置，无须通过set方法.
	 * @param object
	 * @param fieldName
	 * @param value
	 * @throws NoSuchFieldException
	 */
	public static void setFieldValue(Object object, String fieldName, Object value) throws NoSuchFieldException {
		Field field = getDeclaredField(object, fieldName);
		if (!field.isAccessible()) {
			field.setAccessible(true);
		}
		try {
			field.set(object, value);
		} catch (IllegalAccessException e) {
			logger.error("不可能抛出的异常:{}", e.getMessage());
		}
	}

	/**
	 * 循环向上转型,获取对象的DeclaredField.
	 * @param object
	 * @param fieldName
	 * @return
	 * @see #getDeclaredField(Class clazz, String fieldName)
	 * @throws NoSuchFieldException
	 */
	public static Field getDeclaredField(Object object, String fieldName) throws NoSuchFieldException {
		Assert.notNull(object);
		return getDeclaredField(object.getClass(), fieldName);
	}

	/**
	 * 循环向上转型,获取类的DeclaredField.
	 * @param clazz
	 * @param fieldName
	 * @return
	 * @throws NoSuchFieldException
	 */
	@SuppressWarnings("unchecked")
	public static Field getDeclaredField(Class clazz, String fieldName) throws NoSuchFieldException {
		Assert.notNull(clazz);
		Assert.hasText(fieldName);
		for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
				// Field不在当前类定义,继续向上转型
			}
		}
		throw new NoSuchFieldException("No such field: " + clazz.getName() + '.' + fieldName);
	}
	
	/**
	 * 对象间属性的copy
	 * @since 2009-9-22,下午03:22:11
	 * @param dest 目标对象
	 * @param orig 源对象
	 */
	public static synchronized void copyProperties(Object dest, Object orig){
		try {
			PropertyUtils.copyProperties(dest, orig);
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			logger.error("属性拷贝时抛出的异常:{}", e1.getMessage());
		} catch (InvocationTargetException e1) {
			// TODO Auto-generated catch block
			logger.error("属性拷贝时抛出的异常:{}", e1.getMessage());
		} catch (NoSuchMethodException e1) {
			// TODO Auto-generated catch block
			logger.error("属性拷贝时抛出的异常:{}", e1.getMessage());
		}
	}
}
